package java;

public class Question14 {
    public static void main(String[] args) {

        String[] input =  {"flower","flow","flight"};
        System.out.println(longestCommonPrefix(input));
    }
    public static String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length < 1){
            return "";
        }
        if (strs.length == 1){
            return strs[0];
        }
        int minLength = Integer.MAX_VALUE;
        for (int i = 0; i < strs.length ; i ++){
            minLength = Math.min(minLength,strs[i].length());
        }
        StringBuilder stringBuffer = new StringBuilder();
        boolean flag = true;
        for (int index = 0; index < minLength; index ++){
            for (int i = 0; i < strs.length - 1;i ++){
                flag = strs[i].charAt(index) == strs[i + 1].charAt(index);
                if (!flag){
                    break;
                }
            }
            if (flag){
                stringBuffer.append(strs[0].charAt(index));
            }else {
                return stringBuffer.toString();
            }
        }
        return stringBuffer.toString();
    }
}
